package base;

import db.IPersistable;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 *
 * @author Shayan
 */
public class Food extends Entity implements IPersistable {

    public Food() {
    }

    public Food(String foodId, String name, float calories, String categoryId) {
        this.setId(foodId);
        this.setName(name);
        this.setCalories(calories);
        this.setSubcategoryId(categoryId);
    }
    /**
     * name
     */
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    /**
     * Calories
     */
    private float calories;

    public float getCalories() {
        return calories;
    }

    public void setCalories(float calories) {
        this.calories = calories;
    }
    /*********
     * Subcategory Id
     */
    private String subcategoryId;

    public String getSubcategoryId() {
        return subcategoryId;
    }

    public void setSubcategoryId(String catId) {
        this.subcategoryId = catId;
    }

    /**
     * validate
     * @throws ValidationException
     */
    public void validate() throws ValidationException {
    }

    public byte[] getData() throws IOException {
        ByteArrayOutputStream bin = new ByteArrayOutputStream();
        DataOutputStream out = new DataOutputStream(bin);
        out.writeUTF(getId());
        out.writeUTF(name);
        out.writeUTF(subcategoryId);
        out.writeFloat(calories);
        
        out.flush();
        return bin.toByteArray();
    }

    public static Food create(byte[] data) throws IOException {
        Food food = new Food();
        ByteArrayInputStream bin = new ByteArrayInputStream(data);
        DataInputStream in = new DataInputStream(bin);

        food.setId(in.readUTF());
        food.setName(in.readUTF());
        food.setSubcategoryId(in.readUTF());
        food.setCalories(in.readFloat());
        return food;
    }
}
